import datetime
import random
import time
import pymysql


class usersys_db:
    # 处理用户系统 与 数据库 之间的交互
    def __init__(self):
        self.db = pymysql.connect(
            user='test',  # 用户名
            password='test123',  # 密码
            host='192.168.0.125',  # 数据库ip
            port=3306,  # 数据库端口
            database="sam",  # 数据库名称
            charset='utf8mb4')  # 字符编码
        self.cs = self.db.cursor()

    def __del__(self):
        self.cs.close()
        self.db.close()

    def get_cate(self):
        self.cs.execute("select distinct tag from question_sam;")
        data = self.cs.fetchall()
        cate_list = [ i[0] for i in data]
        return cate_list

    def get_question_never(self,kind):
        self.cs.execute(f"select qid,qname,tag from question_sam where tag='{kind}' and last_time is null;")
        data = self.cs.fetchall()
        return list(data)

    def get_question_3day(self,kind,days=-3):
        three_day = datetime.datetime.now()+datetime.timedelta(days=days)
        self.cs.execute(f"select qid,qname,tag from question_sam where tag='{kind}' and last_time < '{three_day}' and is_ok=1")
        data = self.cs.fetchall()
        return list(data)

    def get_question_not_good(self,kind):
        self.cs.execute(f"select qid,qname,tag from question_sam where tag='{kind}' and last_time is not null and is_ok=0;")
        data = self.cs.fetchall()
        return list(data)

    def update_question_status(self,qid,is_ok):
        self.cs.execute(f"update question_sam set is_ok={is_ok},last_time=current_date where qid={qid};")
        self.db.commit()


def choice_question(question_list,nums):
    bingo_list = []
    while len(bingo_list) < nums:
        if len(question_list) == 0:
            break
        bingo = random.choice(question_list)
        question_list.remove(bingo)
        bingo_list.append(bingo)
    return bingo_list



def merge(lists):
    total_list = []
    for i in lists:
        total_list.extend(i)
    return total_list

def need_kind(question_list,needs):
    have_kind = []
    for i in question_list:
        if i[2] not in have_kind:
            have_kind.append(i[2])
    if len(have_kind) == len(needs):
        return 'ok'
    else:
        need = []
        for y in needs:
            if y not in have_kind:
                need.append(y)
        return need


def get_target_question(total,kind):
    lists = []
    for i in total:
        if i[2] == kind:
            lists.append(i)
    return lists




if __name__ == '__main__':
    print('欢迎来到往死里抽小程序')
    never = []
    notok = []
    threeDay = []
    bingo_list = []
    t = usersys_db()
    cates = t.get_cate()
    for i in range(len(cates)):
        print(f'输入{i} 抽取 {cates[i]}')
    choice_cate = input('输入多个使用,分割').split(',')
    cate_list = [] # 用户抽取的分类列表
    for i in choice_cate:
        cate_list.append(cates[int(i)])

    for kind in cate_list:
        never_question = t.get_question_never(kind=kind)
        never.append(never_question)
        notok_question = t.get_question_not_good(kind=kind)
        notok.append(notok_question)
        threeDay_question = t.get_question_3day(kind=kind)
        threeDay.append(threeDay_question)

    # print(never)
    # print(notok)  # 8
    # print(threeDay)   # 3  2
    # 所有的分类  合并
    threeDay = merge(threeDay)
    notok = merge(notok)
    never = merge(never)

    bingo_list.extend(choice_question(threeDay,2))
    bingo_list.extend(choice_question(notok, 3))

    # 10 - 3 - 5  =  2       3
    bingo_list.extend(choice_question(never,10-len(cate_list)-len(bingo_list)))

    while len(bingo_list) < 10:
        res = need_kind(bingo_list, cate_list)
        if  res == 'ok':
            choices_question = choice_question(never,10-len(bingo_list) )
            bingo_list.extend(choices_question)
        else:
            for r in res:
                tar = get_target_question(never,r)
                bingo_list.extend(choice_question(tar,1))
    print(bingo_list)

    index = 1
    for qname in bingo_list:
        print(f'第{index}题：分类：{qname[2]},{qname[1]}')
        index += 1
        flag = input('是否掌握，y 掌握， n还不太行')
        if flag == 'y':
            is_ok = 1
        else:
            is_ok = 0
        t.update_question_status(qid=qname[0],is_ok=is_ok)





    # for i in range(len(never)):
    #     data = choice_question(never[i],5)
    # print(data)









"""
1x1=1	
2x1=2	2x2=4	
3x1=3	3x2=6	3x3=9	
4x1=4	4x2=8	4x3=12	4x4=16	
5x1=5	5x2=10	5x3=15	5x4=20	5x5=25	
6x1=6	6x2=12	6x3=18	6x4=24	6x5=30	6x6=36	
7x1=7	7x2=14	7x3=21	7x4=28	7x5=35	7x6=42	7x7=49	
8x1=8	8x2=16	8x3=24	8x4=32	8x5=40	8x6=48	8x7=56	8x8=64	
9x1=9	9x2=18	9x3=27	9x4=36	9x5=45	9x6=54	9x7=63	9x8=72	9x9=81	
"""
# # a 1,2,3,4,5,6,7,8,9  # range(1,10)
# for a in range(1,10):
#     for b in range(1,a+1):
#         print(f'{a}*{b}={a*b}',end='\t')
#     print()
#
# # 1,2,3,4,5,6,7,8,9
# # 9,8,7,6,5,4,3,2,1
# for a in range(9,0,-1):
#     for b in range(1,a+1):
#         print(f'{a}*{b}={a*b}',end='\t')
#     print()

'''
a       b 
1       1               range(1,2)
2       1,2             range(1,3)
3       1,2,3           range(1,4)
4       1,2,3,4         range(1,5)
5       1,2,3,4,5       range(1,6)
6       1,2,3,4,5,6     range(1,7)

                        range(1,a+1)
'''






# 插入排序
# #
# a = [7,3,5,2,4,4]
# for i in range(1,len(a)-1):
#     for j in range(i-1,-1,-1):
#         print(i,j)

"""
1、选择分类
2、选择10道题

抽题原则：
1、每个分类都有题
2、(没抽过的 last_time )   5
(没有背下来的 is_ok 0 and last_time not null )  3  
(背下来，但以及过了3天的 is_ok = 1 and last_time <= 当前时间-3)  2
"""






